package com.hkemmm.model;


import com.hkemmm.bean.UserInfo;
import com.hkemmm.db.DBProvider;
import com.hkemmm.utils.MyUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class UserModel {

    /**
     * 根据id查询数据
     * @param userID
     * @return  查询结果实体类
     */
    public UserInfo queryUserInfoByID(String userID){
        UserInfo u = null;
        String strSql = "select * from userinfo where isdelete = 0 and userid = ? ";
        u = MyUtils.getInstence(DBProvider.class).query(strSql,new BeanHandler<>(UserInfo.class),userID);

        return u;
    }

    /**
     * 查询数据库所有用户信息，降序排列
     * @return  list<UserInfo></>查询结果集
     */
    public List<UserInfo> queryUserInfoAll(){
        List<UserInfo> list = null;
        String strSql = "select * from userinfo where isdelete = 0 order by userid desc";

        list = MyUtils.getInstence(DBProvider.class).query(
                strSql,new BeanListHandler<>(UserInfo.class));

        return list;
    }


    /**
     * 登陆验证
     * @param userName
     * @param userPsw
     * @return  是否能登录
     */
    public boolean canLogin(String userName, String userPsw) {
        boolean canLogin = false;


        String str = "select * from userinfo where isdelete = 0 and userName = ? and userPsw = ?";

//        List<Map<String, Object>> listMap= MyUtils.getInstence(DBProvider.class).queryListMap(str, userName, userPsw);
        //list<Bean>方式返回结果
        List<UserInfo> listUser = MyUtils.getInstence(
                            DBProvider.class).query(str,new BeanListHandler<>(UserInfo.class),userName,userPsw);
        if(listUser.size()>=1 ){
            canLogin = true;
        }

        return canLogin;
    }

    /**
     * 通过对象添加数据库数据
     * @param userInfo  实体类
     * @return  运行结果
     */
    public int saveUserInfo(UserInfo userInfo){
        int i = 0;
        if(userInfo.getUserid() == null){
            String strSql = "insert into userinfo(userName,userPsw,userRegisteDate) values(?,?,?);";

            i = MyUtils.getInstence(DBProvider.class).execute(
                    strSql,userInfo.getUserName(),userInfo.getUserPsw(),userInfo.getUserRegisteDate());
        }else {
            String strSql = "UPDATE userinfo set username = ? ,userpsw = ? , userRegisteDate = ? where userid = ?";

            i = MyUtils.getInstence(DBProvider.class).execute(strSql,new Object[]{
                    userInfo.getUserName(),userInfo.getUserPsw(),userInfo.getUserRegisteDate(),userInfo.getUserid()
            });
        }



        return i;
    }

    /**
     * 判断用户是否存在
     * @param UserName  判断的用户
     * @return  存在返回true，否则返回false
     */
    public boolean userNameIsExist(String UserName){
        Boolean flag = false;

        String strSql = "select * from userInfo where isdelete = 0 and userName = ?";
        List<UserInfo> list = MyUtils.getInstence(
                DBProvider.class).query(strSql,new BeanListHandler<>(UserInfo.class),UserName);
        if(list.size()>=1){
            flag = true;
        }

        return flag;
    }


    /**
     * 根据userinfo中数据进行查询
     * @param userInfo  userinfobean实体类
     * @return  查询结果集
     */
    public List<UserInfo>  queryUserInfoByParams(UserInfo userInfo){
        List<UserInfo> list = new ArrayList<>();
        List lstParmas = new ArrayList<>();

        StringBuffer strsql = new StringBuffer("select * from userinfo where isdelete = 0 ");
        //String strSql = "select * from userinfo where 1=1";
        if(userInfo != null){
            if(StringUtils.isNotEmpty(userInfo.getUserName())){
                strsql.append(" and userName like ? ");

                lstParmas.add("%" + userInfo.getUserName() + "%");
            }

            if(userInfo.getUserRegisteDate() != null){
                strsql.append(" and UserRegisteDate >= ? ");

                lstParmas.add(MyUtils.coverDateToStr(userInfo.getUserRegisteDate(),"yyyy-MM-dd"));
            }

            if(userInfo.getUserRegisteDate2() != null){
                strsql.append(" and UserRegisteDate <= ? ");

                lstParmas.add(MyUtils.coverDateToStr(userInfo.getUserRegisteDate2(),"yyyy-MM-dd"));
            }
        }

        strsql.append(" order by userid desc;");

        list = MyUtils.getInstence(DBProvider.class).query(
                strsql.toString(),new BeanListHandler<>(UserInfo.class),lstParmas.toArray());

        return list;
    }

    /**
     * 根据用户id物理删除数据
     * @param strID
     * @return
     */
    public static int userDeleteByID(String strID){
        int i = 0;      //默认位0

        String strSql = "delete from userinfo where userid = ?";

        //执行结果
        i = MyUtils.getInstence(DBProvider.class).execute(strSql,strID);

        return i;
    }

    public static int userDeleteByID1(String strID){
        int i = 0;      //默认位0

        String strSql = "update userinfo set isdelete = 1 where userid = ?";

        //执行结果
        i = MyUtils.getInstence(DBProvider.class).execute(strSql,strID);

        return i;
    }
}
